import Vue from 'vue'
import Router from 'vue-router'
import index from '@/pages/index'
import WxAuth from '@/pages/WxAuth'
import productList from '@/pages/productList'
import goodInfo from '@/pages/goodInfo'
import cart from '@/pages/cart'
import classGoods from '@/pages/classGoods'
import usercenter from '@/pages/userCenter'
import addressList from '@/pages/addressList'
import addressEdit from '@/pages/addressEdit'
import store from '@/store'
import {editCart} from '../ajax'
import notFound from '../pages/notFound.vue'
import login from '../pages/login.vue'
import register from '../pages/register.vue'
import clerk from '../pages/clerk.vue'
import creatOrder from '../pages/creatOrder.vue'
import orderStatus from '../pages/orderStatus.vue'
import orderList from '../pages/orderList.vue'
import company from '../pages/company.vue'
import orderDetail from '../pages/orderDetail.vue'
import refund from '../pages/refund.vue'
import comment from '../pages/comment'
import userInfo from '../pages/userInfo'
import qrcode from '../pages/qrcode'
import orderStatistics from '../pages/orderStatistics.vue'
import {Toast} from 'vant'
Vue.use(Router)

const router =  new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: index,
      meta:{
        title:'生鲜商城',
        footer:true,
        isLogin:true
      }
    },
    {
      path:'/login',
      name:'login',
      component:login,
      meta:{
        title:'登录'
      }
    },
    {
      path:'/notFound',
      name:'notFound',
      component: notFound,
      meta:{
        title:'NOT-FOUND'
      }
    },
    {
      path: '/WxAuth',
      name: 'WxAuth',
      component:WxAuth,
      meta:{
        title:'加载中...'
      }
    },
    {
      path:'/register',
      name:'register',
      component:register,
      meta:{
        title:'注册',
        back:true
      }
    },
    {
      path: '/productList',
      name: 'productList',
      component:productList,
      meta:{
        back:true,
        title:'商品列表',
        isLogin:true
      }
    },
    {
      path:'/goodInfo',
      name:'goodInfo',
      component:goodInfo,
      meta:{
        back:true,
        title:'商品详情',
        isLogin:true
      }
    },
    {
      path:'/cart',
      name:'cart',
      component:cart,
      meta:{
        back:true,
        title:'购物车',
        isLogin:true
      }
    },
    {
      path:'/classGoods',
      name:'classGoods',
      component:classGoods,
      meta:{
        back:true,
        title:"商品分类",
        isLogin:true
      }
    },
    {
      path:'/usercenter',
      name:'usercenter',
      component:usercenter,
      meta:{
        back:true,
        title:"个人中心",
        footer:true,
        isLogin:true
      }
    },
    {
      path:'/addressList',
      name:'addressList',
      component:addressList,
      meta:{
        title:'地址管理',
        back:true,
        isLogin:true
      }
    },
    {
      path:'/addressEdit',
      name:'addressEdit',
      component:addressEdit,
      meta:{
        title:'地址新增/修改',
        back:true,
        isLogin:true
      }
    },
    {
      path:'/clerk',
      name:'clerk',
      component:clerk,
      meta:{
        title:'店员管理',
        back:true,
        isLogin:true
      }
    },
    {
      path:'/creatOrder',
      name:'creatOrder',
      component:creatOrder,
      meta:{
        title:'提交订单',
        back:true,
        isLogin:true
      }
    },
    {
      path:'/orderStatus',
      name:'orderStatus',
      component:orderStatus,
      meta:{
        title:'订单生成',
        isLogin:true
      }
    },
    {
      path:'/orderList',
      name:'orderList',
      component:orderList,
      meta:{
        title:'订单列表',
        isLogin:true,
        back:true
      }
    },
    {
      path:'/company',
      name:'company',
      component:company,
      meta:{
        title:'选择公司'
      }
    },
    {
      path:'/orderDetail',
      name:'orderDetail',
      component:orderDetail,
      meta:{
        title:'订单详情',
        isLogin:true,
        back:true
      }
    },
    {
      path:'/refund',
      name:'refund',
      component:refund,
      meta:{
        title:'申请退款',
        isLogin:true,
        back:true
      }
    },
    {
      path:'/comment',
      name:'comment',
      component:comment,
      meta:{
        title:'订单评价',
        isLogin:true,
        back:true
      }
    },
    {
      path:'/userInfo',
      name:'userInfo',
      component:userInfo,
      meta:{
        title:'个人信息',
        isLogin:true,
        back:true
      }
    },
    {
      path:'/qrcode',
      name:'qrcode',
      component:qrcode,
      meta:{
        title:'分享二维码',
        isLogin:true,
        back:true
      }
    },
    {
      path:'/orderStatistics',
      name:'orderStatistics',
      component:orderStatistics,
      meta:{
        title:'商品统计',
        isLogin:true,
        back:true
      }
    },
    {
      path:'*',
      redirect:'/notFound',
      name: 'notFound',
      hidden: true
    },
  ] 
})
router.beforeEach((to, from, next) => {
  if (!/micromessenger/i.test(navigator.userAgent)) {
    next()
    return
  }
  // 不要对 WxAuth 路由进行拦截，不进入 WxAuth 路由就拿不到微信返回的授权 code
  if (!to.meta.isLogin) {
    next()
    return
  }
  if(!to.meta.footer){
    to.meta.footer = false;
  }
  if(!to.meta.back){
    to.meta.back = false;
  }
  let wxUserInfo = store.getters.wxUserInfo
  sessionStorage.setItem('wxRedirectUrl', process.env.NODE_ENV=="development"?window.location.origin:`${window.location.origin}/sxplat/`)
  if (!wxUserInfo.openid) {
    if(to.query.admin_id){
      store.commit('admin_id',to.query.admin_id);
    }
    const data = {
      admin_id:to.query.admin_id?to.query.admin_id:store.getters.admin_id
    }
    // getAppid(data).then(res=>{
      // if(!res){
      //   next('/notFound');
      //   return;
      // }else{
      const url = encodeURIComponent(process.env.NODE_ENV=="development"?`${window.location.origin}/#/WxAuth`:`${window.location.origin}/sxplat/#/WxAuth`)
        window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8527e3e11ca00660&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=${to.query.admin_id}-${to.query.state}#wechat_redirect`
        return;
      // }
    // })
  } else {
    if(!store.getters.token && to.name !== 'index'){
      next('/login');
    }else{
      if(store.getters.wxUserInfo.is_adopt !== 1 && to.name !== 'index'){
        Toast('用户暂未通过公司审核,审核后即可查看全部页面');
        next('/');
      }else{
        if((to.name == 'classGoods'|| to.name == 'productList') && store.getters.wxUserInfo.center_ids.length == 0){
          Toast('用户暂未分配分拣中心,请联系运营商分配后查看')
          next('/');
          return;
        }else{
          console.log(store.getters.local_cart);
          if(to.name == 'cart' && store.getters.local_cart.length){
            const data = {
              goods:JSON.stringify(store.getters.local_cart),
              uid:store.getters.wxUserInfo.id,
              group_id:store.getters.wxUserInfo.group_id,
              role_type:store.getters.wxUserInfo.role_type,
            }
            editCart(data).then(res=>{
                console.log(res);
                store.commit('del_cart');
                next();
            })
          }else{
            next();
          }
        }
      }
    }
  }
})
export default router;